﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XRepair.Models;
using RepositoryUtil.SQL;
using XRepair.Conventions.Enum;
//using MvcXRepairAdmin.ViewModel;

namespace MvcXRepairAdmin.Repository.SQL
{
    public class UserDAO : BaseDAO
    {
        public IEnumerable<User> GetAllActiveUsers()
        {
            string spName = "GetAllActiveUsers";

            DataTable resultTab = null;
            List<User> userList = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;
                resultTab = command.ExecuteDataTable();
            }
            if (resultTab != null && resultTab.Rows.Count > 0)
            {
                userList = this.ConvertDataTableToUserList(resultTab);
            }

            return userList;
        }

        //public bool DeactivateBrandByID(int brandId)
        //{
        //    string spName = "DeactivateBrandByID";

        //    object Rowcount = null;

        //    using (var conn = XRepairConn)
        //    {
        //        conn.Open();

        //        SqlCommand command = new SqlCommand(spName, conn);
        //        command.CommandType = CommandType.StoredProcedure;

        //        command.AddInParameter("ID", brandId);
        //        command.AddOutParameter("Rowcount", -1);
        //        command.ExecuteNonQuery();

        //        Rowcount = command.GetParameterValue("Rowcount");
        //    }

        //    long RowcountValue;

        //    if (long.TryParse(Rowcount.ToString(), out RowcountValue) && RowcountValue != -1)
        //    {
        //        return true;
        //    }

        //    return false;
        //}

        //public bool ActivateUserAccount(string actionvationCode)
        //{
        //    string spName = "ActivateUserAccount";
        //    object Rowcount = null;

        //    using (var conn = XRepairConn)
        //    {
        //        conn.Open();

        //        SqlCommand command = new SqlCommand(spName, conn);
        //        command.CommandType = CommandType.StoredProcedure;

        //        command.AddInParameter("ActivationCode", actionvationCode);

        //        command.AddOutParameter("Rowcount", -1);
        //        command.ExecuteNonQuery();

        //        Rowcount = command.GetParameterValue("Rowcount");
        //    }

        //    long RowcountValue;

        //    if (long.TryParse(Rowcount.ToString(), out RowcountValue) && RowcountValue != -1)
        //    {
        //        return true;
        //    }

        //    return false;
        //}

        //public User GetUserInfoByCredentials(string username, string password)
        //{
        //    string spName = "GetUserInfoByCredentials";

        //    DataTable resultTab = null;
        //    User user = null;

        //    using (var conn = XRepairConn)
        //    {
        //        conn.Open();

        //        SqlCommand command = new SqlCommand(spName, conn);
        //        command.CommandType = CommandType.StoredProcedure;

        //        command.AddInParameter("Username", username);
        //        command.AddInParameter("Password", password);

        //        resultTab = command.ExecuteDataTable();
        //    }

        //    if (resultTab != null && resultTab.Rows.Count > 0)
        //    {
        //        user = this.ConvertDataTableToUserModel(resultTab);
        //    }

        //    return user;
        //}

        //public bool AddNewUser(User model)
        //{
        //    string spName = "AddNewUser";
        //    object newIdentity = null;

        //    using (var conn = XRepairConn)
        //    {
        //        conn.Open();

        //        SqlCommand command = new SqlCommand(spName, conn);
        //        command.CommandType = CommandType.StoredProcedure;

        //        command.AddInParameter("RoleID", model.RoleID);
        //        command.AddInParameter("Username", model.Username);
        //        command.AddInParameter("Password", model.Password);
        //        command.AddInParameter("EmailAddress", model.EmailAddress);
        //        command.AddInParameter("CreatedAt", model.CreatedAt);
        //        command.AddInParameter("CreatedBy", model.CreatedBy);
        //        command.AddInParameter("ActivationCode", model.ActivationCode); 

        //        command.AddOutParameter("NewIdentity", -1);
        //        command.ExecuteNonQuery();

        //        newIdentity = command.GetParameterValue("NewIdentity");
        //    }

        //    long newIdentityValue;

        //    if (long.TryParse(newIdentity.ToString(), out newIdentityValue) && newIdentityValue != -1)
        //    {
        //        return true;
        //    }

        //    return false;
        //}


        private List<User> ConvertDataTableToUserList(DataTable data)
        {
            List<User> users = null;

            if (data != null)
            {
                users = new List<User>();

                foreach (DataRow dr in data.Rows)
                {
                    var serviceModel = new User();

                    serviceModel.ID = SqlUtil<int>.GetValue(dr, "ID");
                    serviceModel.RoleID = SqlUtil<int>.GetValue(dr, "RoleID");
                    serviceModel.Username = SqlUtil<string>.GetValue(dr, "Username");
                    serviceModel.Password = SqlUtil<string>.GetValue(dr, "Password");
                    serviceModel.Salt = SqlUtil<string>.GetValue(dr, "Salt");
                    serviceModel.Alias = SqlUtil<string>.GetValue(dr, "Alias");
                    serviceModel.EmailAddress = SqlUtil<string>.GetValue(dr, "EmailAddress");
                    serviceModel.ActivationCode = SqlUtil<string>.GetValue(dr, "ActivationCode");
                    serviceModel.IsActive = SqlUtil<bool>.GetValue(dr, "IsActive");
                    serviceModel.CreatedAt = SqlUtil<DateTime>.GetValue(dr, "CreatedAt");
                    serviceModel.CreatedBy = SqlUtil<String>.GetValue(dr, "CreatedBy");
                    serviceModel.LastUpdatedAt = SqlUtil<DateTime>.GetValue(dr, "LastUpdatedAt");
                    serviceModel.LastUpdatedBy = SqlUtil<String>.GetValue(dr, "LastUpdatedBy");

                    users.Add(serviceModel);
                }
            }

            return users;
        }
    }
}
